import * as organizationApi from '@/api/organization'

const state = {
  organizationList: []
}
const mutations = {
  SET_ORGANIZATION_LIST(state, newValue) {
    state.organizationList = newValue
  }
}

const actions = {
  async updateOrganizationList({ commit }) {
    const res = await organizationApi.getOrganizationList()
    commit('SET_ORGANIZATION_LIST', res)
  }
}

function dataTransfer(data, pid) {
  return data
    .filter(d => d.pid === pid)
    .map(d => ({
      ...d,
      children: dataTransfer(data, d.id)
    }))
}
const getters = {
  departmentTree(state) {
    return dataTransfer(state.organizationList, '')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
